package cn.bocweb.spinning.view.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.SearchAssociateAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;

public class SelectActivity extends BaseActivity implements View.OnClickListener {


    SearchAssociateAdapter adapter;
    int type;

    @Bind(R.id.product_company_select)
    Spinner productCompanySelect;
    @Bind(R.id.edit_content)
    EditText editContent;
    @Bind(R.id.search_delete)
    RelativeLayout searchDelete;
    @Bind(R.id.search_exit)
    TextView searchExit;
    @Bind(R.id.classify)
    GridView classify;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select);
        ButterKnife.bind(this);

        type = getIntent().getIntExtra(Constant.TO_SEARCH, 0);

        initSpinner();
        initEditText();
        initClickBtn();
        initHot();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_exit: {
                finish();
                break;
            }
            case R.id.search_delete: {
                Intent intent = new Intent(SelectActivity.this, SearchResultActivity.class);
                intent.putExtra(Constant.TO_SEARCH_RESULT, type);
                intent.putExtra(Constant.TO_SEARCH_KEYWORD, editContent.getText().toString());
                startActivity(intent);
                break;
            }
        }
    }

    private void initClickBtn() {
        searchDelete.setOnClickListener(this);
        searchExit.setOnClickListener(this);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void initSpinner() {
        productCompanySelect.setDropDownVerticalOffset(DeviceUtil.dip2px(this, 50));
        productCompanySelect.setSelection(type - 1);
        productCompanySelect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0: {
                        type = Constant.PRODUCT;
                        break;
                    }
                    case 1: {
                        type = Constant.COMPANY;
                        break;
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void initHot() {
        adapter = new SearchAssociateAdapter(this);
        classify.setAdapter(adapter);

        classify.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

    private void initEditText() {
        editContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE){
                    Intent intent = new Intent(SelectActivity.this, SearchResultActivity.class);
                    intent.putExtra(Constant.TO_SEARCH_KEYWORD, editContent.getText().toString());
                    intent.putExtra(Constant.TO_SEARCH_RESULT, type);
                    startActivity(intent);
                }
                return false;
            }
        });
    }
}
